<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      color:red;
    }
  </style>
</head>
<body>
  <div id="app">
    <router-link to="/home" tag="button">首页</router-link>
    <router-link to="/news" tag="button">新闻</router-link>
    <button @click="enterAbout">到关于我们</button>

    <button @click="addNewRoute">添加一个新的路由</button>
    <button @click="enterJoin">跳转到这个新的路由</button>
    <button @click="$router.go(0)">刷新</button>
    <router-view/>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script>
    // 创建 路由组件option
    const Home = {
      template: `
        <div>
          <h2>首页</h2>  
        </div>
      `
    }
    const News = {
      template: `
        <div>
          <h2>新闻</h2>  
          <hr/>
          <router-view></router-view>
        </div>
      `
    }
    const About = {
      template: `
        <div>
          <h2>关于我们</h2>  
        </div>
      `
    }
    const NotFound =  {
      template: `
        <div>
          <h2>404了</h2>  
        </div>
      `
    }
    const routes = [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        name: '首页',
        component: Home
      },
      {
        path: '/news',
        name: '新闻',
        component: News
      },
      {
        path: '/about',
        name: '关于我们',
        component: About
      },
      {
        path: '*',
        component: NotFound
      }
    ]
    //  创建router 实例
    const router = new VueRouter({
      routes
    })
    // 准备一个新的路由组件options
    const Join = {
      template: `
        <div>
          <h2>加入我们</h2>  
        </div>
      `
    }
    const vm = new Vue({
      el: '#app',
      router,
      methods: {
        addNewRoute(){
          this.$router.addRoute({
            path: '/join',
            name: '加入我们',
            component: Join
          })
          // 作为二级路由插入
          /* this.$router.addRoute(
            '新闻',
            {
              path: '/join',
              name: '加入我们',
              component: Join
            }
          ) */
        },
        enterJoin(){
          this.$router.push('/join')
        },
        enterAbout(){
          this.$router.replace('/about')
        }
      },
      mounted(){
        console.log(this.$router, 111);
      }
    })
  </script>
</body>
</html>